<template>
	<view class="main p30">
		<view class="lists ">
			<view @tap="select(v,i)" v-for="(v,i) in data" :key="i" class="box bgw selectAct">
				<image v-show="v.act == false" class="select" src="../../static/image/nomal@2x.png"></image>
				<image v-show="v.act == true" class="select" src="../../static/image/select@2x.png"></image>
				<image class="shop" :src="v.thumbnail" mode=""></image>
				<view class="txt">
					<p class="p1 text-df two">
						{{v.name}}
					</p>
					<p class="p3 ">
						<view> ¥<span>{{v.price}}</span></view>
					</p>
				</view>
			</view>
		</view>
		<view class="bottoms">
			<view class="ffl">
				已选中 <span class="c2"> {{ids.length}} </span> 件
			</view>
			<u-button @tap="success()" :ripple="true" shape="circle" type="success">选好了</u-button>
		</view>
	</view>
</template>

<script>
	import {
		myProducts
	} from '../../api/api.js'
	export default {
		data() {
			return {
				state: 1,
				data: [],
				pageNumber: 1,
				pageSize: 10,
				stops: 0,
				selects: [],
				ids: [],
				sum: 9,
				AssociatedGoods: []
			};
		},
		created(e) {
			this.AssociatedGoods = uni.getStorageSync('AssociatedGoods')
			this.init()
		},
		onReachBottom() {
			if (this.stops == 0) {
				this.init()
			}
		},
		methods: {
			success() {
				let arr = []
				for (let i = 0; i < this.data.length; i++) {
					if (this.data[i].act == true) {
						this.data[i].name = this.data[i].name.slice(0,6)+'...'
						arr.push(this.data[i]) 
					}
				}
				let AssociatedGoods = {
					ids: this.ids,
					goods: arr,
					type: 0
				}
				uni.setStorageSync('AssociatedGoods', AssociatedGoods)
				uni.navigateBack()
			},
			select(v, i) {
				if (this.ids.indexOf(v.id) == -1) {
					if (this.ids.length < this.sum) {
						this.data[i].act = true
						this.ids.push(v.id)
					} else {
						this.$u.toast(`最多选择${this.sum}`);
					}

				} else {
					this.data[i].act = false
					this.ids.splice(this.ids.indexOf(v.id), 1)
				}
			},
			async init() {
				let parameter = {
					pageable: {
						pageNumber: this.pageNumber,
						pageSize: this.pageSize
					},
					isShow: false
				}
				let data = await myProducts(parameter)
				data = data.data
				for (let i = 0; i < data.length; i++) {
					if (this.AssociatedGoods.ids) {
						if (this.AssociatedGoods.ids.indexOf(data[i].id) == -1) {
							data[i].act = false
						} else {
							data[i].act = true
							this.ids.push(data[i].id)
						}
					} else {
						data[i].act = false
					} 
				}
				if (data.length != this.pageSize) {
					this.stops = 1
				}
				this.data = [...this.data, ...data]
				this.pageNumber = this.pageNumber + 1
			}
		}
	}
</script>

<style lang="less" scoped>
	@import './index.less';
</style>
